
$(function() {
	
	// $("input[id^='dept']:visible").autocomplete(Test.dept);
	
	$('#birthday').datepicker(_date1_option);
	
	$('#indate').datepicker(_date1_option);

	$("#dept").tooltip({
		items: "#dept",
		content: function() {
			return '请输入部门名称拼音首字母，然后选择并回车即可。如行政，输入xz';
		},
		
		position : {
			my : "left top",
			at : "right bottom+5"
		}
		
	});
	
	$("#dept").autocomplete(Test.dept);
	
	$('textarea').css("width", 400);
	
	/*
	$("#code_t").click(function() {
		_code_t($(this));
	});
	
	_code_t($("#code_t"));
	*/
	
	_change("input[id='source']:visible", "select[id^='source_t']:visible");
	
	var _vv_option = {
		rules: {
			idcard: {
				remote: "emp.php?do=Emp.ajax_exist_idcard",
				required: true
			}
		},
		messages: {
			idcard: {
				remote: jQuery.format("{0} 已经存在")
			}
		},
		errorPlacement: function(label, element) {
			label.insertAfter(element);
		},
		success: function(label) {
			label.html("&nbsp;").addClass("checked");
		}
	};	
	
	validator = $("#form_add").validate(_vv_option);
	
	$("#idcard").change(function(form) {		
		var body = { };
		body.idcard = $(this).val();
		
		if (!idcard(body.idcard)) {
			alert("请输入正确的15位或18位身份证号码！");
			return;
		}
			
		var url = 'emp.php?do=Info.ajax_idcard';
		$.getJSON(url, body, function(json) {
			// alert($.toJSON(json));
			$("#birthday").val(json.birthday);
			$("#sex").val(json.sex);
			
			$("#province").val(json.province);
			$("#city").val(json.city);
			$("#district").val(json.region);
			
			$("#fzjg").val(json.region + "公安局");
		});
    });
	
	$("#mobile").change(function(form) {
		var s = $(this).val();
		_mobile(s);
	});
	
	$("#office").change(function(form) {
		var s = $("#mobile").val();
		_mobile(s);
	});
	
	_mobile($("#mobile").val());
	
	$('#idcard').focus().keypress(function(event){
       if (event.keyCode == 13) {
    	   $('#nationality').focus();
       }
       
       var body = { };
		body.idcard = $(this).val();
			
		var url = 'emp.php?do=Info.ajax_idcard';
		$.getJSON(url, body, function(json) {
			// alert($.toJSON(json));
			$("#birthday").val(json.birthday);
			$("#sex").val(json.sex);
			
			$("#province").val(json.province);
			$("#city").val(json.city);
			$("#district").val(json.region);
		});
	});
	
	$("input[name=emp]:visible").autocomplete(Test.emp);
	
	_ck_default("input[id='emp']:visible", "input[id^='t_emp']:visible");
	
	_ck_default("input[id='im_openfire']", "input[id^='im_t']:visible");
});

function _mobile(s) {
	
	var regex = /^1(3|4|5|7|8)(\d{9})$/
	if (!regex.test(s)) {
		return false;
	}
	
	var dh4 = s.substring(7, 11);
	
	// alert(dh4);
	
	$select = $("#dh_t");
	$select.empty();
	var count = 0;
	var aa = ['-', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69'];
	$.each(aa, function(k, v) {
		// alert(k);
		var dh = v + dh4;
		var item;
		if (v == '-') {
			item = new Option('请选择', k);
		} else {
			item = new Option(dh, dh);
		}
		
		$select[0].options.add(item);
		count++;
	});
	
	$select.val("-");
	
	_change("input[id='short']:visible", "select[id^='dh_t']:visible");
	
	_exten(s);
}

function _exten(s) {
	
	var dh3 = s.substring(8, 11);
	
	$select = $("#exten_t");
	$select.empty();
	var count = 0;
	var aa = ['-', '2', '6', '8'];
	$.each(aa, function(k, v) {
		// alert(k);
		var dh = v + dh3;
		var item;
		if (v == '-') {
			item = new Option('请选择', k);
		} else {
			item = new Option(dh, dh);
		}
		
		$select[0].options.add(item);
		count++;
	});
	
	$select.val("-");
	_change("input[id='exten']:visible", "select[id^='exten_t']:visible");
}

function _mobilebbbb(s) {
	var regex = /^1(3|4|5|7|8)(\d{9})$/
	if (regex.test(s)) {
		$("#short").val('68' + s.substring(7, 11));
		
		var exten = '';
		// var t = $("#office").find("option:selected").text();
		var v = $("#office").find("option:selected").val();
		if (v == '2') {
			exten = "8" + s.substring(8, 11);
		} else if (v == '3') {
			exten = "2" + s.substring(8, 11);
		} else {
			exten = "6" + s.substring(8, 11);
		}
		
		$("#exten").val(exten);
	}
}

function _code_t(c) {
	var s = $("#code");
	if (c.attr("checked")) {
		c.val("1");
		s.attr("disabled", true);
	} else {
		c.val("2");
		s.attr("disabled", false);
	}
}
